package com.wedo.mall;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 处理Android6.0运行时权限
 * Created by lxy on 2017/1/7.
 */

public class WPermission {
    private Activity mActivity;
    private List<String> mPermissions;
    public int mRequestCode = 1001;

    public static WPermission with(Activity activity){
        return new WPermission(activity);
    }

    public WPermission(Activity activity) {
        this.mActivity = activity;
        mPermissions = new ArrayList<>();
    }

    public WPermission camera(){
        return permission(Manifest.permission_group.CAMERA);
    }

    public WPermission location(){
        return permission(Manifest.permission_group.LOCATION);
    }

    public WPermission microphone(){
        return permission(Manifest.permission_group.MICROPHONE);
    }

    public WPermission db(){
        return permission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    public WPermission permission(String permission){
        if (!mPermissions.contains(permission)){
            mPermissions.add(permission);
        }
        return this;
    }

    public WPermission permissions(String[] permissions){
        List<String> list = Arrays.asList(permissions);
        mPermissions.addAll(list);
        return this;
    }

    public WPermission permissions(List<String> permissions){
        mPermissions.addAll(permissions);
        return this;
    }

    public WPermission requestCode(int requestCode){
        mRequestCode = requestCode;
        return this;
    }

    public WPermission request(){
        if (mPermissions.isEmpty()){
            return this;
        }
        ActivityCompat.requestPermissions(mActivity,mPermissions.toArray(new String[mPermissions.size()]),mRequestCode);
        return this;
    }

    public static boolean hasPermission(Context context,String permission){
        return ContextCompat.checkSelfPermission(context, permission)
                == PackageManager.PERMISSION_GRANTED;
    }

    public static boolean hasCameraPermission(Context context){
        return hasPermission(context,Manifest.permission_group.CAMERA);
    }

    public static boolean hasLocationPermission(Context context){
        return hasPermission(context,Manifest.permission_group.LOCATION);
    }

    public static boolean hasMicrophonePermission(Context context){
        return hasPermission(context,Manifest.permission_group.MICROPHONE);
    }

    public static boolean hasDbPermission(Context context){
        return hasPermission(context,Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == mRequestCode){
            List<String> granteds = new ArrayList<>();
            List<String> denieds = new ArrayList<>();

            for (int i = 0;i<grantResults.length;i++){
                int result = grantResults[i];
                if (result == PackageManager.PERMISSION_GRANTED){
                    granteds.add(permissions[i]);
                }else if (result == PackageManager.PERMISSION_DENIED){
                    denieds.add(permissions[i]);
                }
            }

            if (mListener !=null){
                mListener.onGranted(granteds);
                mListener.onDenied(denieds);
            }
        }
    }

    public List<String> getPermissions(){
        return mPermissions;
    }

    private OnPermissionListener mListener;
    public WPermission listener(OnPermissionListener listener){
        this.mListener = listener;
        return this;
    }
    public interface OnPermissionListener{
        void onGranted(List<String> granteds);
        void onDenied(List<String> denieds);
    }

}
